package com.yzy.soft.system.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;
import com.yzy.soft.common.persistence.dao.MaxnoMapper;
import com.yzy.soft.common.persistence.model.Maxno;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.base.tips.Tip;
import com.yzy.soft.system.dto.TableDataDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-02-02
 */
@RestController
@RequestMapping("/maxno")
public class MaxnoController extends BaseController {
    @Autowired
    private MaxnoMapper maxnoMapper;


    @GetMapping("")
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize) {
        Wrapper<Maxno> wrapper = new EntityWrapper<Maxno>();
        page = (page - 1) * pageSize;
        pageSize = page + pageSize;
        List<Maxno> result = maxnoMapper.selectPage(new RowBounds(page, pageSize), wrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        //tableDataDto.setCount(businessMapper.getCount().intValue());

        tableDataDto.setCount(maxnoMapper.selectCount(wrapper));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    @GetMapping("/by")
    public TableDataDto getCadtion(@ModelAttribute Maxno maxno) {
        Wrapper<Maxno> wrapper = new EntityWrapper<Maxno>();
        wrapper.where("versionId={0}", maxno.getId());

        List<Maxno> result = maxnoMapper.selectList(wrapper);
        TableDataDto tableDataDto = new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        //tableDataDto.setCount(businessMapper.getCount().intValue());

        tableDataDto.setCount(result.size());
        tableDataDto.setData(result);
        return tableDataDto;
    }


    @PostMapping("")
    public Tip add(@ModelAttribute Maxno maxno) {
        maxno.setCreate(new Date());
        maxno.setUpdate(new Date());
        Integer result = maxnoMapper.insert(maxno);
        return SUCCESS_TIP;
    }

    @DeleteMapping("")
    public Tip delete(@ModelAttribute Maxno maxno) {
        Integer result = maxnoMapper.deleteById(maxno.getId());
        return SUCCESS_TIP;
    }
    @DeleteMapping("/{id}")
    public Tip delete(@RequestParam(name="ids[]",required = false) String[] maxno,@PathVariable(name="id",required = false) String id) {
        if(StringUtils.isNotBlank(id)){
            Integer result=maxnoMapper.deleteById(id);
        }else if(maxno.length!=0){
            Integer result=maxnoMapper.deleteBatchIds(Lists.newArrayList(maxno) );
        }
        return SUCCESS_TIP;
    }

    @PutMapping("")
    public Tip update(@ModelAttribute Maxno maxno) {
        maxno.setUpdate(new Date());
        Integer result = maxnoMapper.updateById(maxno);
        return SUCCESS_TIP;
    }
}
